package net.findkrypton;

import java.util.Arrays;

import net.findkrypton.enume.Level;
import net.findkrypton.po.SaveFile;
import net.jumpengine.Manager;
import net.jumpengine.util.UComm;

/**
 * 游戏氪金界面计算
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年3月4日 下午2:34:59<br />
 * @date
 */
public class GameInfo {
	private final int top=80,one=24;
	private Level level;
	private int[][] info=new int[][] {
		{15,20,70},
		{40,25,210},
		{50,30,330}
	};
	private int[] array;
	private int x,y,width,height;
	private int hcount,vcount,kcount;
	
	public GameInfo(Level level) {
		this.level=level;
		math();
	}
	
	public void setLevel(Level level){
		this.level=level;
		math();
	}
	
	public void reset() {
		math();
	}
	
	public void reload(SaveFile save,int[] ints) {
		level=save.getLevel();
		hcount=save.getWidth();
		vcount=save.getHeight();
		kcount=info[level.getVal()][2];
		array=ints;
		
		width=hcount*one;
		height=vcount*one;
		
		x=(Manager.getWidth()-width)/2;
		y=(Manager.getHeight()-height-top)/2;
		y+=top;
	}
	
	private void math() {
		hcount=info[level.getVal()][0];
		vcount=info[level.getVal()][1];
		kcount=info[level.getVal()][2];
		
		width=hcount*one;
		height=vcount*one;
		
		x=(Manager.getWidth()-width)/2;
		y=(Manager.getHeight()-height-top)/2;
		y+=top;
		Manager.getLog().info(GameInfo.class, "[math]x=",x," y=",y," width=",width," height=",height," lecel=",level);
		
		array=new int[hcount*vcount];
		Arrays.fill(array,-1);
		int count=0,tmp;
		while(count<kcount) {
			tmp=UComm.rnd(0, array.length-1);
			
			if(array[tmp]==-1) {
				array[tmp]=-2;
				count++;
			}
		}

		for(int i=0;i<array.length;i++) {
			if(array[i]==-2) {
				array[i]=18;
				continue;
			}
			array[i]=countOne(i,-2,18)+20;
		}
	}
	
	public int countOne(int index,int...nums) {
		int c=0,tmp;
		if(index % hcount !=0) {
			tmp=index-hcount-1;
			if(in(tmp) && inArray(array[tmp],nums))c++;
		}
		tmp=index-hcount;
		if(in(tmp) && inArray(array[tmp],nums))c++;
		if((index-(hcount-1)) % hcount !=0) {
			tmp=index-hcount+1;
			if(in(tmp) && inArray(array[tmp],nums))c++;
		}
		
		if(index % hcount !=0) {
			tmp=index-1;
			if(in(tmp) && inArray(array[tmp],nums))c++;
		}
		if((index-(hcount-1)) % hcount !=0) {
			tmp=index+1;
			if(in(tmp) && inArray(array[tmp],nums))c++;
		}
		
		if(index % hcount !=0) {
			tmp=index+hcount-1;
			if(in(tmp) && inArray(array[tmp],nums))c++;
		}
		tmp=index+hcount;
		if(in(tmp) && inArray(array[tmp],nums))c++;
		if((index-(hcount-1)) % hcount !=0) {
			tmp=index+hcount+1;
			if(in(tmp) && inArray(array[tmp],nums))c++;
		}
		return c;
	}
	
	private boolean inArray(int ind,int... nums) {
		if(nums.length==1) {
			return ind==nums[0];
		}
		for(int num:nums) {
			if(ind==num)return true;
		}
		return false;
	}
	
	private boolean in(int val) {
		return val>=0 && val<array.length;
	}

	public Level getLevel() {
		return level;
	}

	public int[] getArray() {
		return array;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public int getOne() {
		return one;
	}

	public int getHcount() {
		return hcount;
	}

	public int getVcount() {
		return vcount;
	}

	public int getKcount() {
		return kcount;
	}
}
